// pages/user/company/companyProperty/companyProperty.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: null,
        companyId: null,
        recordList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.time) {
            this.setData({
                companyId: options.company,
                time: options.time
            })
        } else {
            this.setData({
                companyId: options.company
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getRecordList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getRecordList()
    },

    getRecordList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let recordList = this.data.recordList
        if (this.data.time == null) {
            let data = {
                filiale_id: 1,
                limit: 10,
                page: this.data.nowPage
            }
            common.post('Data/property_list', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        recordList: recordList,
                        isAll: true
                    })
                } else {
                    recordList = recordList.concat(res)
                    that.setData({
                        recordList: recordList,
                        nowPage: that.data.nowPage + 1
                    })
                }
            }).catch(function(e) {})
        } else {
            let data = {
                time: this.data.time,
                filiale_id: 1,
                limit: 10,
                page: this.data.nowPage
            }
            common.post('Data/property_list', data).then(function(res) {
                that.setData({
                    isLoading: false
                })
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        recordList: recordList,
                        isAll: true
                    })
                } else {
                    recordList = recordList.concat(res)
                    that.setData({
                        recordList: recordList,
                        nowPage: that.data.nowPage + 1
                    })
                }
            }).catch(function(e) {})
        }
    },

    goOrderDetail: function(e) {
        if (e.currentTarget.dataset.type == 2) {
            wx.showModal({
                title: '提示',
                content: '此订单为配额购买订单！',
                showCancel: false,
                success(res) {}
            })
        } else {
            wx.navigateTo({
                url: '/pages/order/orderDetail/orderDetail?orderId=' + e.currentTarget.dataset.id + '&dignity=admin'
            })
        }
    }
})